/***************************************************************************
 * The Open Armory API is a project to retrieve World of Warcraft(R) 
 * related XML data from The Armory.
 * 
 * The data retrieved via the Open Armory API from The Armory is 
 * proprietary to Blizzard Entertainment, Inc(R).
 * *************************************************************************
 * Copyright (C) 2009 - 2010 Tedla M.B. Brandsema
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/
package net.openarmory.core.domain;

import java.io.InputStream;

import net.openarmory.core.domain.constants.ArmoryContentGroup;
import net.openarmory.core.domain.constants.ArmoryIconSize;
import net.openarmory.core.domain.constants.ArmoryPortraitLevel;
import net.openarmory.core.domain.constants.ArmoryZone;
import net.openarmory.core.domain.constants.CharacterClass;
import net.openarmory.core.domain.constants.CharacterGender;
import net.openarmory.core.domain.constants.CharacterRace;
import net.openarmory.core.domain.url.ArmoryIconUrl;
import net.openarmory.core.domain.url.ArmoryImageUrlBuilder;
import net.openarmory.core.domain.url.ArmoryPortraitUrl;
import net.openarmory.core.domain.url.ArmoryUrl;
import net.openarmory.core.domain.url.ArmoryUrlFactory;
import net.openarmory.core.domain.url.UrlConnectionReader;

/**
 * The class {@code ArmoryImageRetriever} retrieves different image types from
 * The Armory.
 */
public final class ArmoryImageRetriever extends UrlConnectionReader {

	/** The image URL builder. */
	private final ArmoryImageUrlBuilder imageUrlBuilder;

	/** The armory URL. */
	private ArmoryUrl armoryUrl;

	/**
	 * This method returns the {@link ArmoryUrl} that is instantiated by the
	 * {@link #fetchIcon(ArmoryZone, ArmoryIconSize, String)} or
	 * {@link #fetchPortrait(ArmoryZone, ArmoryPortraitLevel, CharacterGender, CharacterRace, CharacterClass)}
	 * methods.The method returns {@code null} if either
	 * {@link #fetchIcon(ArmoryZone, ArmoryIconSize, String)} or
	 * {@link #fetchPortrait(ArmoryZone, ArmoryPortraitLevel, CharacterGender, CharacterRace, CharacterClass)}
	 * have not been called first.
	 * 
	 * @return The image specific armory URL.
	 */
	public ArmoryUrl getArmoryUrl() {
		return this.armoryUrl;
	}

	/**
	 * Instantiates a new armory image retriever.
	 */
	public ArmoryImageRetriever() {
		this.imageUrlBuilder = (ArmoryImageUrlBuilder) ArmoryUrlFactory
				.getUrlBuilder(ArmoryContentGroup.IMAGE);
	}

	/**
	 * Method for fetching icons from The Armory.
	 * 
	 * @param zone
	 *            The zone represents The Armory region (i.e. EU or US).
	 * @param iconSize
	 *            The size of the icon to retrieve (i.e. 21, 43 or 64 pixels).
	 * @param iconName
	 *            The name of the icon to retrieve.
	 * @return The {@code InputStream} containing the icon.
	 */
	public InputStream fetchIcon(final ArmoryZone zone,
			final ArmoryIconSize iconSize, final String iconName) {
		final ArmoryIconUrl iconUrl = this.imageUrlBuilder.getIconUrl(zone,
				iconSize, iconName);
		this.armoryUrl = iconUrl;
		return fetchContent(iconUrl.getUrl());
	}

	/**
	 * Method for fetching portraits from The Armory.
	 * 
	 * @param zone
	 *            The zone represents The Armory region (i.e. EU or US).
	 * @param portraitLevel
	 *            The portrait level represents the character level which is
	 *            associated with the portrait (i.e. 80, 70, 60 or default).
	 * @param gender
	 *            The gender of the portrait character.
	 * @param race
	 *            The race of the portrait character.
	 * @param clazz
	 *            The class of the portrait character.
	 * @return The {@code InputStream} containing the portrait.
	 */
	public InputStream fetchPortrait(final ArmoryZone zone,
			final ArmoryPortraitLevel portraitLevel,
			final CharacterGender gender, final CharacterRace race,
			final CharacterClass clazz) {
		final ArmoryPortraitUrl portraitUrl = this.imageUrlBuilder
				.getPortraitUrl(zone, portraitLevel, gender, race, clazz);
		this.armoryUrl = portraitUrl;
		return fetchContent(portraitUrl.getUrl());
	}

}
